|
|
|
|
|
|
|
|
Built-in collections may not necessarily contain all of the following properties and methods.  |
|
|
|
|
|
|
| Table 16.2 Properties and Methods Common to All Collections | | Item | Use To | | Count property | Report the number of objects in a collection | | Item method | Get a single object from a collection |
|
|
|
|
|
|
In addition to the items in Table 16.2, collections usually provide two more methods. The methods in Table 16.3 are common to most collections. |
|
|
|
|
| Table 16.3 Methods Common to Most Collections | | Method | Purpose | | Add | Add an object to a collection | | Remove | Delete an object from a collection |
|
|
|
|
|
|
The Add and Remove methods provide programmers a standard way to create and delete items in a collection. The Visual Basic Forms and Controls collections are maintained by Visual Basic, so they don't support these methods. The Add method usually includes key and index parameters, used to identify an object in a collection. However, Add and Remove may be implemented differently for different types of collections. For example, compare the Add syntax for the ListView control's ListItems collection and the ListImages collection of an image list: |
|
|
|
|
|
|
|
|
ListView1.ListItems.Add index,key,text,IconNumber,smallIconNumber
ImageList1.ListImages.Add index,key,picture |
|
|
|
|
|
|
|
|
Creating a New Collection for Grouped Actions |
|
|
|
|
|
|
|
|
You can create collections to contain forms, controls, and objects. Use the Collection object data type when creating a new collection. The following declaration creates a new collection named colSelected: |
|
|
|
|
|
|
|
|
Dim colSelected As New Collection |
|
|
|
|
|
|
|
|
Declaring a variable as a Collection object gives you four built-in properties and methods, as shown in Table 16.4. |
|
|
|
|
|